// Registry.cpp : implementation file
//

#include "pch.h"
#include "framework.h"
#include "Registry.h"

/////////////////////////////////////////////////////////////////////////////
// CRegistry

CRegistry::CRegistry(HKEY hKey)
{
	m_hKey=hKey;
}

CRegistry::~CRegistry()
{
	Close();
}

/////////////////////////////////////////////////////////////////////////////
// CRegistry Functions

BOOL CRegistry::CreateKey(LPCTSTR lpSubKey)
{
	ASSERT(m_hKey);
	ASSERT(lpSubKey);

	HKEY hKey;
	DWORD dw;
	//REG_OPTION_NON_VOLATILE
	//This key is not volatile; this is the default. The information is stored in a file and is preserved when the system is restarted.
	long lReturn=RegCreateKeyEx(m_hKey,lpSubKey,0L,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dw);
    
	if(lReturn==ERROR_SUCCESS)
	{
		m_hKey=hKey;
		return TRUE;
	}
	
	return FALSE;
	
}

BOOL CRegistry::Open(LPCTSTR lpSubKey)
{
	ASSERT(m_hKey);
	ASSERT(lpSubKey);
    
	HKEY hKey;
	long lReturn=RegOpenKeyEx(m_hKey,lpSubKey,0L,KEY_ALL_ACCESS,&hKey);
    
	if(lReturn==ERROR_SUCCESS)
	{
        m_hKey=hKey;
		return TRUE;
	}
	return FALSE;
	
}

void CRegistry::Close()
{
	if(m_hKey)
	{
		RegCloseKey(m_hKey);
		m_hKey=NULL;
	}
	
}

BOOL CRegistry::DeleteValue(LPCTSTR lpValueName)
{
	ASSERT(m_hKey);
	ASSERT(lpValueName);
	
	long lReturn=RegDeleteValue(m_hKey,lpValueName);
	
	if(lReturn==ERROR_SUCCESS)
		return TRUE;
	return FALSE;
	
}

BOOL CRegistry::DeleteKey(HKEY hKey, LPCTSTR lpSubKey)
{
	ASSERT(hKey);
	ASSERT(lpSubKey);
	
	long lReturn=RegDeleteValue(hKey,lpSubKey);
	
	if(lReturn==ERROR_SUCCESS)
		return TRUE;
	return FALSE;
	
}

BOOL CRegistry::Write(LPCTSTR lpSubKey, int nVal)
{
	ASSERT(m_hKey);
	ASSERT(lpSubKey);
	
	DWORD dwValue;
	dwValue=(DWORD)nVal;
	
	long lReturn=RegSetValueEx(m_hKey,lpSubKey,0L,REG_DWORD,(const BYTE *) &dwValue,sizeof(DWORD));
	
   	if(lReturn==ERROR_SUCCESS)
		return TRUE;
	
	return FALSE;
	
}

BOOL CRegistry::Write(LPCTSTR lpSubKey, DWORD dwVal)
{
	ASSERT(m_hKey);
	ASSERT(lpSubKey);
	
	long lReturn=RegSetValueEx(m_hKey,lpSubKey,0L,REG_DWORD,(const BYTE *) &dwVal,sizeof(DWORD));
	
   	if(lReturn==ERROR_SUCCESS)
		return TRUE;
	
	return FALSE;
	
}

BOOL CRegistry::Write(LPCTSTR lpValueName, LPCTSTR lpValue)
{
	ASSERT(m_hKey);
	ASSERT(lpValueName);
	ASSERT(lpValue);	

	long lReturn=RegSetValueEx(m_hKey,lpValueName,0L,REG_SZ,(const BYTE *) lpValue, strlen((const char *)lpValue)+1);
	
   	if(lReturn==ERROR_SUCCESS)
		return TRUE;
	
	return FALSE;
	
}


BOOL CRegistry::Read(LPCTSTR lpValueName, int* pnVal)
{
	ASSERT(m_hKey);
	ASSERT(lpValueName);
	ASSERT(pnVal);
	
	DWORD dwType;
	DWORD dwSize=sizeof(DWORD);
	DWORD dwDest;
	long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)&dwDest,&dwSize);
	
	if(lReturn==ERROR_SUCCESS)
	{
		*pnVal=(int)dwDest;
		return TRUE;
	}
	return FALSE;
	
}

BOOL CRegistry::Read(LPCTSTR lpValueName, DWORD* pdwVal)
{
	ASSERT(m_hKey);
	ASSERT(lpValueName);
	ASSERT(pdwVal);
	
	DWORD dwType;
	DWORD dwSize=sizeof(DWORD);
	DWORD dwDest;
	long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)&dwDest,&dwSize);
	
	if(lReturn==ERROR_SUCCESS)
	{
		*pdwVal=dwDest;
		return TRUE;
	}
	return FALSE;
	
}




BOOL CRegistry::RestoreKey(LPCTSTR lpFileName)
{
	ASSERT(m_hKey);
	ASSERT(lpFileName);
	
	long lReturn=RegRestoreKey(m_hKey,lpFileName,REG_WHOLE_HIVE_VOLATILE);
	
	if(lReturn==ERROR_SUCCESS)
		return TRUE;
	
	return FALSE;
}

BOOL CRegistry::SaveKey(LPCTSTR lpFileName)
{
	ASSERT(m_hKey);
	ASSERT(lpFileName);
	
	long lReturn=RegSaveKey(m_hKey,lpFileName,NULL);
	
	if(lReturn==ERROR_SUCCESS)
		return TRUE;
	
	return FALSE;
}


BOOL CRegistry::Read(LPCTSTR lpValueName, CString* lpVal)
{
	ASSERT(m_hKey);
	ASSERT(lpValueName);
	ASSERT(lpVal);
	
	DWORD dwType;
	DWORD dwSize=200;
	char szString[2550];
	
	long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)szString,&dwSize);
	
	if(lReturn==ERROR_SUCCESS)
	{
		*lpVal=szString;
		return TRUE;
	}
	return FALSE;
	
}

CString CRegistry::get_string(CString key)
{
	CRegistry reg;
	CString str_ret=_T("");
	BOOL bReturn;
	bReturn= reg.Open (_T("Software\0"));
	bReturn= reg.CreateKey (_T("SJHUaSoft\0"));
	bReturn= reg.Open (_T("SJHUaSoft\0"));
	bReturn= reg.CreateKey (_T("SFC\0"));
	bReturn= reg.Open (_T("SFC\0"));
	reg.Read((LPCTSTR)key, &str_ret);
	return str_ret;
}

void CRegistry::set_string(CString key, CString value)
{
	CRegistry reg;
	BOOL bReturn;
	bReturn = reg.Open(_T("Software\0"));
	bReturn = reg.CreateKey(_T("SJHUaSoft\0"));
	bReturn = reg.Open(_T("SJHUaSoft\0"));
	bReturn = reg.CreateKey(_T("SFC\0"));
	bReturn = reg.Open(_T("SFC\0"));
	reg.Write((LPCTSTR)key, (LPCTSTR)value);

}

CString CRegistry::get_mc_r1_value(CString key)
{
	return get_string(key);
}

void CRegistry::set_mc_r1(CString key, CString value)
{
	set_string(key, value);
}

int CRegistry::get_integer(CString key)
{
	CRegistry reg;
	DWORD u32_ret=0;
	BOOL bReturn;
	bReturn = reg.Open(_T("Software\0"));
	bReturn = reg.CreateKey(_T("SJHUaSoft\0"));
	bReturn = reg.Open(_T("SJHUaSoft\0"));
	bReturn = reg.CreateKey(_T("SFC\0"));
	bReturn = reg.Open(_T("SFC\0"));
	reg.Read((LPCTSTR)key, &u32_ret);
	return u32_ret;
}

void CRegistry::set_integer(CString key, int value)
{
	CRegistry reg;
	BOOL bReturn;
	bReturn = reg.Open(_T("Software\0"));
	bReturn = reg.CreateKey(_T("SJHUaSoft\0"));
	bReturn = reg.Open(_T("SJHUaSoft\0"));
	bReturn = reg.CreateKey(_T("SFC\0"));
	bReturn = reg.Open(_T("SFC\0"));
	reg.Write((LPCTSTR)key, value);
}


double CRegistry::get_double(CString key)
{
	double db_ret = 0;
	CString str_value = get_string(key);
	db_ret = _ttof(str_value.GetBuffer(0));
	return db_ret;
}

void CRegistry::set_double(CString key, double value)
{
	
}
